package com.lzh.redisdemo.repository;

import com.lzh.redisdemo.pojo.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @Classname StudentJpaRepository
 * @Description JpaRepository接口：该接口是最常用的接口
 * JpaSpecificationExecutor接口: 用于实现动态条件查询
 * @Date 2021/9/27 11:22
 * @Created by liuzihao
 */
public interface StudentJpaRepository extends JpaRepository<Student, Integer>, JpaSpecificationExecutor<Student> {

    /**
     * 注解式查询
     */
    //默认使用JPQL作为查询语句，该JPQL查询语句与HQL查询语句一致，查询的是类名和属性名（严格区分大小写）
    @Query("from Student where name like %?1%")
    List<Student> findStudentByName(String studentName);

    //根据学生的姓名和地址进行模糊查询
    @Query("from Student where name like %?1% and address like %?2%")
    List<Student> findStudentByNameAndAddress(String studentName, String address);

    //使用SQL语句写
    @Query(value = "select * from Student where name like %?1% and address like %?2%", nativeQuery = true)
    List<Student> findStudentList(String studentName, String address);

    /**
     * 根据姓名和地址模糊查询
     *
     * @param studentName
     * @param address
     * @return
     */
    @Query("from Student where name like %:name% and address like %:address%")
    List<Student> findStudent(@Param("name") String studentName, @Param("address") String address);

    @Modifying
    @Query("update Student set name = ?1 where id = ?2")
    int updateStudent(String studentName, Integer id);

    @Modifying
    @Query("delete from Student where id = ?1")
    int deleteStudent(Integer id);

}
